package edu.utn.tacs.grupouno.ui.client;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.utn.tacs.grupouno.ui.client.dto.AnalisisDetalleDTO;

public class AnalisisDetallePanel extends VerticalPanel {

	private String id;
	public AnalisisDetallePanel(String id) {
		this.id = id;
		initComponents();
	}

	private void initComponents() {
		final Panel me = this;
		me.add(new Label("Cargando"));
		AnalisisUIServiceAsync service = GWT.create(AnalisisUIService.class);
		service.getDetalledAnalisis(id, new AsyncCallback<AnalisisDetalleDTO>() {
			@Override
			public void onSuccess(AnalisisDetalleDTO analisis) {
				me.clear();
				Label title = new Label("Detalles Analisis");
				title.setStyleName("title");
				me.add(title);
				
				HorizontalPanel medPanel = new HorizontalPanel();
				Label medTitle = new Label("Medico:");
				medTitle.setStyleName("subTitle");
				medPanel.add(medTitle);
				medPanel.add(new Label(analisis.getNombreMedico()));
				me.add(medPanel);
				
				HorizontalPanel pacPanel = new HorizontalPanel();
				Label pacTitle = new Label("Paciente:");
				pacTitle.setStyleName("subTitle");
				pacPanel.add(pacTitle);
				pacPanel.add(new Label(analisis.getNombrePaciente()));
				me.add(pacPanel);
				
				HorizontalPanel lisPanel = new HorizontalPanel();
				Label lisTitle = new Label("Completo:");
				lisTitle.setStyleName("subTitle");
				lisPanel.add(lisTitle);
				lisPanel.add(new Label(analisis.isEstaListo() ? "Si" : "No"));
				me.add(lisPanel);
				
				me.add(new Label(""));
				
				Label estTitle = new Label("Estudios: ");
				estTitle.setStyleName("title");
				me.add(estTitle);
				
				final VerticalPanel estudioContainer = new VerticalPanel();
				
				VerticalPanel container = new VerticalPanel();
				List<String> idsEstudiosCargados = analisis.getIdsEstudiosCargados();
				int i = 1;
				for(String idEstudio : analisis.getIdsEstudios()) {
					Hyperlink link = new Hyperlink();
					Panel row = new HorizontalPanel();
					link.setText("Ver detalles");
					link.addClickHandler(new EstudioClickHandler(idEstudio, estudioContainer));
					row.add(new Label(String.valueOf(++i)));
					row.add(new Label(" - "));
					row.add(new Label(idsEstudiosCargados.contains(idEstudio) ? "Cargado" : "Pendiente"));
					
					row.add(new Label(" - "));
					row.add(link);
					container.add(row);
					
				}
				
				me.add(container);
				
				me.add(new Label(""));
				me.add(estudioContainer);
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				me.clear();
				me.add(new ErrorLabel("Error: " + arg0.getMessage()));
			}
		});
	}
	
	private class EstudioClickHandler implements ClickHandler {
		private String id;
		private Panel target;
		public EstudioClickHandler(String id, Panel target) {
			this.id = id;
			this.target = target;
		}

		@Override
		public void onClick(ClickEvent arg0) {
			target.clear();
			target.add(new EstudioPanel(id));
		}
	}
}
